package cn.supermarket.common.config;

import cn.supermarket.common.core.entity.LoginUser;
import cn.supermarket.common.entity.system.SysDictData;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * caffeine缓存配置
 * @Author yp
 * @Date 2025/2/19:08:59
 */
@Configuration
public class CaffeineConfig
{
    /**
     * 不过期数据字典缓存
     * @return
     */
    @Bean
    @Qualifier(value = "dictCache")
    public LoadingCache<String, List<SysDictData>> dictCache(){
        return Caffeine.newBuilder()
                .build(new CacheLoader<String, List<SysDictData>>() {
                    @Nullable
                    @Override
                    public List<SysDictData> load(@NonNull String s) throws Exception {
                        return null;
                    }

                    @Override
                    public List<SysDictData> reload(String key, List<SysDictData> oldValue) throws Exception {
                        return oldValue;
                    }
                });
    }

    /**
     * 不过期系统配置缓存
     * @return
     */
    @Bean
    @Qualifier(value = "sysConfigCache")
    public LoadingCache<String, Object> sysConfigCache(){
        return Caffeine.newBuilder()
                .build(new CacheLoader<String, Object>() {
                    @Nullable
                    @Override
                    public Object load(String s) throws Exception {
                        return null;
                    }
                });
    }

    /**
     * 可过期登录用户缓存
     * @return
     */
    @Bean
    @Qualifier(value = "userCache")
    public LoadingCache<String, LoginUser> userCache(){
        return Caffeine.newBuilder()
                .expireAfterAccess(3600L, TimeUnit.SECONDS)
                .build(new CacheLoader<String, LoginUser>() {
                    @Nullable
                    @Override
                    public LoginUser load(String s) throws Exception {
                        return null;
                    }
                });
    }

    /**
     * 可过期验证码缓存
     * @return
     */
    @Bean
    @Qualifier(value = "captchaCache")
    public LoadingCache<String, String> captchaCache(){
        return Caffeine.newBuilder()
                .expireAfterAccess(60L, TimeUnit.SECONDS)
                .build(new CacheLoader<String, String>() {
                    @Nullable
                    @Override
                    public String load(String s) throws Exception {
                        return null;
                    }
                });
    }

    /**
     * 可过期登录用户缓存
     * @return
     */
    @Bean
    @Qualifier(value = "urlCache")
    public LoadingCache<String, String> urlCache(){
        return Caffeine.newBuilder()
                .expireAfterAccess(5L, TimeUnit.SECONDS)
                .build(new CacheLoader<String, String>() {
                    @Nullable
                    @Override
                    public String load(String s) throws Exception {
                        return null;
                    }
                });
    }
}
